package roborally.program.command;

import roborally.itemsonboard.Robot;
import roborally.program.text.TextCommandPattern;
import be.kuleuven.cs.som.annotate.Basic;
import be.kuleuven.cs.som.annotate.Immutable;

/**
 * A class representing a while-statement.
 * This consists of a condition and a body.
 * The body is executed as long as the condition is true.
 * 
 * @author Molderez Tom, 2de bach Ingenieurswetenschappen Computerwetenschappen-Elektrotechniek and Reniers Jorn, 2de bach Ingenieurswetenschappen Werktuigkunde-Computerwetenschappen
 * @version 1.0
 *
 */
public class WhileCommand extends ConditionalCommand {

	private static final String WHILE  = "while";
	
	/**
	 * Creates a new WhileCommand.
	 * 
	 */
	public WhileCommand(Condition condition) {
		super(condition);
	}

	/**
	 * Execute this WhileCommand.
	 */
	@Override
	public void execute(Robot robot) {
		do {
			this.getCondition().execute(robot);
			if(this.getCondition().getReturnValue())
				super.execute(robot);
		} while(this.getCondition().getReturnValue());
	}

	/**
	 * Return a String representation of this WhileCommand.
	 */
	@Override
	public String write() {
		String write = new String();
		
		write = write + " (" + this.getCondition().write() + ")";
		write = write + super.write();
		
		return "(" + WHILE + write + ")";
	}
}
